﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class Customer
    {
        private ISessionFactory _sessionFactory;
        public Customer()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.Customer)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListQuickSearch(string condition)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "select * from Customer where Customer_Id + Customer_Name like N'%" + condition + "%' and Status = 'CUR'";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.Customer));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.Customer GetObjectByPK(string cusId)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Customer where Customer_ID =? and Status = 'CUR'").AddEntity(typeof(DataObjects.Customer));
                query.SetString(0, cusId);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.Customer)arrTemp[0];
                }
                return null;
            }
        }

        public int GetMaxID()
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Customer order by Customer_Id desc").AddEntity(typeof(DataObjects.Customer));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                DataObjects.Customer oCus;

                if (arrTemp.Count > 0)
                {
                    oCus = (DataObjects.Customer)arrTemp[0];
                    if (oCus != null)
                    {
                        return int.Parse(oCus.CustomerId);
                    }
                }
                return 0;
            }
        }

        public ArrayList SearchCustomer(object[] oParam)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "exec sp_SearchCustomer ?, ?, ?, ?, ?, ?";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.Customer));
                query.SetString(0, oParam[0].ToString());                   //Customer_Id
                query.SetString(1, oParam[1].ToString());                   //Customer_Name
                query.SetString(2, oParam[2].ToString());                   //Person_Represent
                query.SetString(3, oParam[3].ToString());                   //Person_Contact
                query.SetString(4, oParam[4].ToString());                   //Kind_Of_Customer
                query.SetString(5, oParam[5].ToString());                   //Status

                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.Customer oCus)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oCus);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.Customer oCus)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oCus);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string cusId)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.Customer oCus = (DataObjects.Customer)session.Load(typeof(DataObjects.Customer), cusId);
                session.Delete(oCus);
                tx.Commit();
            }
        }
    }
}
